﻿using System;
using Avalonia;
using dpa.Library.Services;
using dpa.Library.ViewModels;
using dpa.Services;
using Microsoft.Extensions.DependencyInjection;

namespace dpa;

public class ServiceLocator
{
    private readonly IServiceProvider _serviceProvider;

    private static ServiceLocator _current;

    public static ServiceLocator Current
    {
        get
        {
            if (_current is not null)
            {
                return _current;
            }

            if (Application.Current.TryGetResource(nameof(ServiceLocator), null, out var resource)
                && resource is ServiceLocator serviceLocator)
            {
                return _current = serviceLocator;
            }

            throw new Exception("理论上不会出现这种情况");
        }
    }

    public ResultViewModel ResultViewModel => _serviceProvider.GetRequiredService<ResultViewModel>();
    public TodayViewModel TodayViewModel => _serviceProvider.GetRequiredService<TodayViewModel>();
    public MainWindowViewModel MainWindowViewModel => _serviceProvider.GetRequiredService<MainWindowViewModel>();
    public MainViewModel MainViewModel => _serviceProvider.GetRequiredService<MainViewModel>();
    public QueryViewModel QueryViewModel => _serviceProvider.GetRequiredService<QueryViewModel>();
    public FavoriteViewModel FavoriteViewModel => _serviceProvider.GetRequiredService<FavoriteViewModel>();
    public TodayDetailViewModel TodayDetailViewModel => _serviceProvider.GetRequiredService<TodayDetailViewModel>();
    public DetailViewModel DetailViewModel => _serviceProvider.GetRequiredService<DetailViewModel>();


    public IRootNavigationService RootNavigationService =>
        _serviceProvider.GetRequiredService<IRootNavigationService>();

    public InitializationViewModel InitializationViewModel =>
        _serviceProvider.GetRequiredService<InitializationViewModel>();

    public ServiceLocator()
    {
        var serviceCollection = new ServiceCollection();

        serviceCollection.AddSingleton<ResultViewModel>();
        serviceCollection.AddSingleton<TodayViewModel>();
        serviceCollection.AddSingleton<MainWindowViewModel>();
        serviceCollection.AddSingleton<MainViewModel>();
        serviceCollection.AddSingleton<QueryViewModel>();
        serviceCollection.AddSingleton<FavoriteViewModel>();
        serviceCollection.AddSingleton<InitializationViewModel>();
        serviceCollection.AddSingleton<TodayDetailViewModel>();
        serviceCollection.AddSingleton<DetailViewModel>();

        serviceCollection.AddSingleton<IPoetryStorage, PoetryStorage>();
        serviceCollection.AddSingleton<IPreferenceStorage, FilePreferenceStorage>();
        serviceCollection.AddSingleton<ITodayPoetryService, JinrishiciService>();
        serviceCollection.AddSingleton<IAlertService, AlertService>();
        serviceCollection.AddSingleton<IRootNavigationService, RootNavigationService>();
        serviceCollection.AddSingleton<IMenuNavigationService, MenuNavigationService>();
        serviceCollection.AddSingleton<IContentNavigationService, ContentNavigationService>();
        serviceCollection.AddSingleton<ITodayImageService, BingImageService>();
        serviceCollection.AddSingleton<ITodayImageStorage, TodayImageStorage>();
        serviceCollection.AddSingleton<IFavoriteStorage, FavoriteStorage>();


        _serviceProvider = serviceCollection.BuildServiceProvider();
    }
}